﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SVN.Net.Models
{
    public partial class SVN
    {
        public class ItemCollection : List<Item>
        {
            public string Repo
            {
                get;
                private set;
            }

            public ItemCollection(string repo)
                : base()
            {
                Repo = repo;
            }
        }

        public class Item : IComparer<Item>
        {

            #region Properties

            public bool IsFile
            {
                get;
                private set;
            }

            public string Name
            {
                get;
                private set;
            }

            public long Size
            {
                get;
                private set;
            }

            public long Reversion
            {
                get;
                private set;
            }

            public string Author
            {
                get;
                private set;
            }

            public DateTime Date
            {
                get;
                private set;
            }

            public string URL
            {
                get
                {
                    return GlobalApplication.Combine(Parent.Repo, Name);
                }
            }

            public ItemCollection Parent
            {
                get;
                private set;
            }

            #endregion

            public Item()
            {
            }

            public Item(System.Xml.XmlNode entry, ItemCollection parent)
            {
                Parent = parent;
                IsFile = (string.Compare(entry.Attributes["kind"].Value, "dir", true) != 0);
                Name = entry.SelectSingleNode("name").InnerText;
                long size, ver;
                if (entry.SelectSingleNode("size") != null)
                    long.TryParse(entry.SelectSingleNode("size").InnerText, out size);
                else
                    size = 0;
                Size = size;
                var commit = entry.SelectSingleNode("commit");
                long.TryParse(commit.Attributes["revision"].Value, out ver);
                Reversion = ver;
                Author = commit.SelectSingleNode("author").InnerText;
                DateTime date;
                DateTime.TryParse(commit.SelectSingleNode("date").InnerText, out date);
                Date = date;
            }

            #region IComparer<Item> Members

            public int Compare(Item x, Item y)
            {
                int ret = 0;
                if (x.IsFile == y.IsFile)
                {
                    ret = string.Compare(x.Name, y.Name);
                }
                else
                {
                    ret = x.IsFile ? 1 : -1;
                }
                return ret;
            }

            #endregion
        }
    }
}
